﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
// Copyright (C) 2021 kingerrobot
// 版权所有。 
//
// 文件名：BWcsSprayCount
// 文件功能描述：
//
// 创建者：名字 (zzl)
// 时间：2021/12/30 9:59:59
//
// 修改人：
// 时间：
// 修改说明：
// 版本：V1.0.0
//----------------------------------------------------------------*/
#endregion
namespace KingerRobot.MexicoSailunMDC.SpiService;

internal class BWcsSprayCount : IWcsSprayCount
{
    private readonly static IWmsSetting _WmsSetting = new BWmsSetting();
    private readonly static ITask _Task = new BTask();
    private readonly static IProdSku _ProdSku = new BProdSku();
    private readonly static IRcsEquipCuring _RcsEquipCuring = new BRcsEquipCuring();
    private readonly static IWcsEquipLoc _WcsEquipLoc = new BWcsEquipLoc();
    public int IsWcsSprayCountByType(long taskId)
    {
        // 获取任务
        var task = _Task.GetTaskByTaskId(taskId);
        var targetLoc = task.TargetLoc ?? 1;

        // 配对硫化模
        var otherLoc = _RcsEquipCuring.GetOtherHalfCuring(targetLoc);
        if (otherLoc == null)
        {
          
            return 0;
        }

        // 配对硫化模任务是否喷涂(是：配对胎胚也喷涂)
        //WriteLog($"{targetLoc}是否喷涂 查看{otherLoc}硫化模是否喷涂.");
        var otherTask = _Task.GetAllTasksByTargetLoc(otherLoc.GetValueOrDefault()).FirstOrDefault();
        if(otherTask is not null && otherTask.IsSpray == 1)
        {
            return 1;
        }


        // (次)喷涂比例，达到指定数喷涂一次
        var wmsSetting = _WmsSetting.GetWmsSettingByCode(ConstFields.SprayCount);
        _ = int.TryParse(wmsSetting?.Value, out var number);

        // 1:全部出库数量；2:物料规格数量；3:硫化机左右模数量（左模硫化右模必须硫化）
        wmsSetting = _WmsSetting.GetWmsSettingByCode(ConstFields.SprayType);
        _ = int.TryParse(wmsSetting?.Value, out var type);

        long objectId = 1; // 默认全部
        // 物料ID，硫化机，
        var num = 1L;
        if(type == (int)EnumSprayType.Vulcanizing)
        {
            objectId = targetLoc;
        }
        else if (type == (int)EnumSprayType.Material)
        {
            var skuId = task?.SkuId;
            var prodSku = _ProdSku.GetProdSkuById(skuId);
            objectId = prodSku?.MaterialId ?? 1;
        }
        //GetWcsSprayCount(type, objectId); 原先逻辑lsq
        num = GetWcsSprayCount(type, objectId);

       
        return num % number == 0 ? 1 : 0;
    }
    public int IsWcsSprayCountByTypeV2(long taskId)
    {
        // 获取任务
        var task = _Task.GetTaskByTaskId(taskId);
        var targetLoc = task.TargetLoc ?? 1;

        // 配对硫化模
        var otherLoc = _RcsEquipCuring.GetOtherHalfCuring(targetLoc);
        if (otherLoc == null)
        {
           
            return 0;
        }

        // 配对硫化模任务是否喷涂(是：配对胎胚也喷涂)
     
        var otherTask = _Task.GetAllTasksByTargetLoc(otherLoc.GetValueOrDefault()).FirstOrDefault();
        if (otherTask is not null && otherTask.IsSpray == 1)
        {
            return 1;
        }
        var curingLoc = _WcsEquipLoc.GetEquipLocByLocId(targetLoc);
        var curing = _RcsEquipCuring.GetCuringByCuringId(curingLoc.EquipId.GetValueOrDefault());
        _RcsEquipCuring.UpdateCuringSpare(curing.CuringCode);
        var curingSpare = _RcsEquipCuring.GetCuringSpares().Where(t => t.CuringCode == curing.CuringCode).FirstOrDefault();
        if (curingSpare == null)
        {
            return 0;
        }
        var num = curingSpare.SpareCurrentNum;
        var number = curingSpare.SpareNum;
        return num % number == 0 ? 1 : 0;
    }


    private long GetWcsSprayCount(int type, long? objectId)
    {
        // 修改
        UpdateWcsSprayCount(type, objectId);

        // 获取
        var wcsSprayCount = GetWcsSprayCountByType(type, objectId);

        if(wcsSprayCount == null)
        {
            AddWcsSprayCount(type, objectId);
            return 1;
        }
        return wcsSprayCount.Count.GetValueOrDefault();

    }

    private POWcsSprayCount GetWcsSprayCountByType(int type, long? objectId)
    {
        var db = Db.Default;
        var result = db.WcsSprayCounts
            .Where(t => t.Type == type && t.Objectid == objectId)
            .FirstOrDefault();
        return result;
    }

    private int AddWcsSprayCount(int type, long? objectId)
    {
        var db = Db.Default;

        var result = db.WcsSprayCounts.Insert(new POWcsSprayCount
        {
            Id = XID.Nextval,
            Type = type,
            Objectid = objectId
        });

        return result;
    }

    private int UpdateWcsSprayCount(int type, long? objectId)
    {

        var db = Db.Default;
        var result = 0;
        var sprayCount = GetWcsSprayCountByType(type, objectId);
        if (sprayCount != null)
        {
             db.WcsSprayCounts
                .Where(t => t.Type == type && t.Objectid == objectId)
                .Update( new POWcsSprayCount()
            {
                Count = sprayCount.Count + 1,
            });
           result= db.SaveChanges();
        }
        else
        {
            result = AddWcsSprayCount(type, objectId);
        }
        return result;
    }
}